package com.artron.mediaartron.ui.helper;

import android.util.Log;
import android.view.View;

import com.jakewharton.rxbinding.view.RxView;
import com.trello.rxlifecycle.LifecycleTransformer;
import com.trello.rxlifecycle.components.RxActivity;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.trello.rxlifecycle.components.support.RxFragment;

import java.util.concurrent.TimeUnit;

import rx.Observer;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-11-29.
 */

public class ThrottleFirstClickHelper {

    private static final String TAG = "ThrottleFirstClickHelpe";

    public static void throttleFirst(final View view, final View.OnClickListener callback) {
        LifecycleTransformer<Void> transformer = null;
        if (callback instanceof RxFragment) {
            transformer = ((RxFragment) callback).bindToLifecycle();
        } else if (callback instanceof RxAppCompatActivity) {
            transformer = ((RxAppCompatActivity) callback).bindToLifecycle();
        }
        if (transformer != null) {
            RxView.clicks(view).throttleFirst(1, TimeUnit.SECONDS)
                    .compose(transformer)
                    .subscribe(new Observer<Void>() {
                        @Override
                        public void onCompleted() {
                            Log.d(TAG, "onCompleted: ");
                        }

                        @Override
                        public void onError(Throwable e) {
                            Log.d(TAG, "onError: ");
                        }

                        @Override
                        public void onNext(Void aVoid) {
                            callback.onClick(view);

                        }

                    });
        }
    }
}
